// pages/biz/wallet/index/index.js
import Serv from '../walletServ'

var regeneratorRuntime = require('../../../../lib/js/runtime-module.js')
import config from '../../../../utils/config2'

Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    bindwallet: null,
    show: false,
    opacity: 'rgba(255,255,255,0)',
    certification:false,
    chatValue:'',
    commissionDetail:{}, // 佣金信息
  },
  getMoney(amount) {
    if(amount==null){
        return "";
    }
    // 金额转化为分为单位
    // 处理包含, ￥ 或者$的金额
    let currency =  amount.replaceAll("\\$|\\￥|\\,", "");
    let index = currency.indexOf(".");
    let length = currency.length();
    let amLong = 0;
    if(index == -1){
        amLong = Long.valueOf(currency+"00");
    }else if(length - index >= 3){
        amLong = Long.valueOf((currency.substring(0, index+3)).replace(".", ""));
    }else if(length - index == 2){
        amLong = Long.valueOf((currency.substring(0, index+2)).replace(".", "")+0);
    }else{
        amLong = Long.valueOf((currency.substring(0, index+1)).replace(".", "")+"00");
    }
    return amLong.toString();
},
  bindconfirm(){

   /* let openid = wx.getStorageSync('openId');
    var opt = {
      appid: "wx200ae2633309d211",
      mch_id: "1494425362",
      key: "WtZSXgGeE9PH8vq26zULYVpRCY9pGQmA",
      notify_url: "https://app-zmzj-uat.mideazy.com/h5/pwd/index.html"
   };
   var wxpay = new WxPay(opt);
  
   var attach = "343434";
       var body = "2232323";
       var bookingNo =Math.random().toString(36).substr(2,15);//订单号长度有限制具体看微信API文档
       var total_fee = 10//0.1元
    	 var ip = '127.0.0.1';
       wxpay.order(attach, body, openid, bookingNo, total_fee, ip).then(function(data){
         if(data){
          wx.requestPayment({
            ...data,
            success:()=>{
              console.log('支付成功');
            },
            fail: function (res) {
              console.log(res);
              return;
          }
          });
         }
       
       });*/
  },
  //确定绑定钱包
  confirmwallet() {
    wx.redirectTo({
      url: `/subPackages2/pages/wallet/openWallet/openWallet`,
    });
    this.setData({
      show: true,
    })
  },
  cancelwallet() {
    console.log(111)
    this.setData({
      show: false,
    })
  },
  // 查询
  getWalletUser: async function (user_id) {
    //发起请求 user_id  user_type
    let params = {
      accountId: user_id,
      // appId: '',
    }
    let {success, msg, data} = await Serv.getWalletUser(params)
    
    if (!success) {
      wx.showToast({
        title: msg || '',
        icon: 'none',
      })
      return false
    }
   
    if (!data) {//未开通钱包
       this.setData({
         show:true,
         opacity: 'rgba(0,0,0,0.5)'
       })
      // wx.showModal({
      //   title: '提示',
      //   content: '请开通钱包！',
      //   // showCancel: false,
      //   success: function (res) {
      //     if (res.confirm) {
      //       wx.navigateTo({
      //         url: '../openWallet/openWallet',
      //       })
      //     } else {
      //     wx.switchTab({
      //       url:'../../my/mine/mine'
      //     })
      //     }
      
      //   }
      // })
    }
    //本地保存钱包基本信息
    if(data){
      let { name = '', balance = '', certification, idCardNo, psettleBalance, psettleFrozen, remitTime,llWalletId}=data||{}
      let mobile = wx.getStorageSync(config.phoneNoKey)||''
      mobile=mobile.replace(/(\d{3})\d*(\d{4})/, '$1****$2')
      let newPsettleFrozen = (Number(psettleBalance) + Number(balance)).toFixed(2)
      //debugger
      wx.setStorageSync('walletInfo', {
        name, mobile, balance, idCardNo, psettleBalance, psettleFrozen:newPsettleFrozen,llWalletId
      })
      remitTime = remitTime.slice(0,10)
      let total = balance + psettleBalance + psettleFrozen;
      this.setData({
        balance, certification, psettleBalance, psettleFrozen: newPsettleFrozen, remitTime,total
      })
    }
    
  },
  // 余额查询

  //获取佣金详情
  async getCommissionDetail(){
    let { data=null,success } = await Serv.getCommissionDetail({})
    // data.data=[{},{},{},{},{},{},{}]
    if(success&&data){
      this.setData({ commissionDetail: data })
    }else{
      this.setData({ commissionDetail: { data: []} })
    }
  },
  hide() {
    this.setData({
      hidden: !this.data.hidden
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let user_id = wx.getStorageSync(config.userIdKey)
    this.setData({user_id})
     //获取佣金详情
     this.getCommissionDetail()
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let user_id = wx.getStorageSync(config.userIdKey)
    this.getWalletUser(user_id)
  },
  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },
  
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },
  
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  
  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  //
  // }
})
